package top.mccat.web.controller.employee;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.mccat.common.annotation.Log;
import top.mccat.common.core.controller.BaseController;
import top.mccat.common.core.domain.AjaxResult;
import top.mccat.common.core.domain.entity.SysUser;
import top.mccat.common.enums.BusinessType;
import top.mccat.common.utils.poi.ExcelUtil;
import top.mccat.common.core.page.TableDataInfo;
import top.mccat.system.domain.EmployeePunch;
import top.mccat.system.domain.EmployeeVacation;
import top.mccat.system.entity.PunchEntity;
import top.mccat.system.entity.VacationEntity;
import top.mccat.system.service.IEmployeePunchService;
import top.mccat.system.service.ISysUserService;
import top.mccat.web.controller.tool.DateUtils;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2022-03-30
 */
@RestController
@RequestMapping("/system/punch")
public class EmployeePunchController extends BaseController
{
    @Autowired
    private IEmployeePunchService employeePunchService;
    @Autowired
    private ISysUserService sysUserService;
    @Value("${date.start}")
    private String arriveLateTime;
    @Value("${date.end}")
    private String leaveEarlyTime;

    /**
     * 查询【员工考勤记录】列表
     */
    @PreAuthorize("@ss.hasPermi('system:punch:list')")
    @GetMapping("/list")
    public TableDataInfo list(EmployeePunch employeePunch)
    {
        startPage();
        List<EmployeePunch> list = employeePunchService.selectEmployeePunchList(employeePunch);
        List<PunchEntity> punchEntities = new ArrayList<>();
        for(EmployeePunch punch : list){
            SysUser sysUser = sysUserService.selectUserById(punch.getUserId());
            PunchEntity vacationEntity = new PunchEntity(sysUser,punch);
            punchEntities.add(vacationEntity);
        }
        return getDataTable(punchEntities);
    }

    /**
     * 导出【员工考勤记录】列表
     */
    @PreAuthorize("@ss.hasPermi('system:punch:export')")
    @Log(title = "【员工考勤】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmployeePunch employeePunch)
    {
        List<EmployeePunch> list = employeePunchService.selectEmployeePunchList(employeePunch);
        ExcelUtil<EmployeePunch> util = new ExcelUtil<EmployeePunch>(EmployeePunch.class);
        util.exportExcel(response, list, "【员工考勤】数据");
    }

    /**
     * 获取【员工考勤记录】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:punch:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(employeePunchService.selectEmployeePunchById(id));
    }

    /**
     * 新增【员工考勤记录】
     */
    @PreAuthorize("@ss.hasPermi('system:punch:add')")
    @Log(title = "【新增员工考勤记录】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmployeePunch employeePunch)
    {
        return toAjax(employeePunchService.insertEmployeePunch(employeePunch));
    }

    /**
     * 修改【员工考勤记录】
     */
    @PreAuthorize("@ss.hasPermi('system:punch:edit')")
    @Log(title = "【修改员工考勤记录】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmployeePunch employeePunch)
    {
        Date startTime = employeePunch.getStartTime();
        Date endTime = employeePunch.getEndTime();
        if(startTime==null || endTime==null){
            return AjaxResult.error(AjaxResult.StatusCode.ExtraError.getCode(),"开始打卡时间或结束打卡时间不能为空！");
        }
        //逻辑如下，如果正常上班修改，则设置为早退，反之设置为迟到，如果正常上下班则将早退设置为正常，反之判断是否迟到，如果迟到则设置迟到并早退，反之读取默认早退状态
        if(DateUtils.workPunchCheck(startTime,arriveLateTime)){
            employeePunch.setPunchStatus(EmployeePunch.PunchStatus.ArriveLate.getStatus());
        }else {
            employeePunch.setPunchStatus(EmployeePunch.PunchStatus.EarlyLeave.getStatus());
        }
        if(DateUtils.workPunchCheck(endTime,leaveEarlyTime)){
            if(EmployeePunch.PunchStatus.ArriveLate.getStatus()!=employeePunch.getPunchStatus()){
                employeePunch.setPunchStatus(EmployeePunch.PunchStatus.Normal.getStatus());
            }
        }else{
            if(EmployeePunch.PunchStatus.ArriveLate.getStatus()==employeePunch.getPunchStatus()){
                employeePunch.setPunchStatus(EmployeePunch.PunchStatus.ArriveLateAndLeaveEarly.getStatus());
            }
        }
        return toAjax(employeePunchService.updateEmployeePunch(employeePunch));
    }

    /**
     * 删除【员工考勤记录】
     */
    @PreAuthorize("@ss.hasPermi('system:punch:remove')")
    @Log(title = "【删除员工考勤记录】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(employeePunchService.deleteEmployeePunchByIds(ids));
    }
}
